package com.server.db.bean;

import com.sandking.tools.SK_Map;
import java.util.ArrayList;
import com.alibaba.fastjson.JSON;
import java.util.Map;
import com.server.db.imp.YhImp;
import com.sandking.io.SK_ByteBuff;
import com.sandking.tools.SK_Plus;
import java.util.List;
import java.util.HashMap;
import com.server.db.imp.YhzsImp;

/**
 * 用户装饰
 */
public class Yhzs {

	public static final String TABLE_NAME = "用户装饰";
	public static final String CLASS_NAME = "Yhzs"; 
	static final int MAX_GET = Short.MAX_VALUE;
	
	//索引名称
	public static final String  Id_IndexName = "id";
	public static final String  Yh_id_IndexName = "yh_id";
	
	//对象版本号	
	private int _v = 0;
	public int _nextVer(){
		if(_v > MAX_GET) _v = 0;
		return ++_v;
	}
	
	public int _getVer(){
		return _v;
	}
	
	//是否是源对象
	private boolean _source = false;
	
	public boolean is_source() {
		return _source;
	}

	public void set_source(boolean _source) {
		this._source = _source;
	}
	
	//Cache 中的索引
	private String  id_Unique = null;
	private String  yh_id_Index = null;
	public Yhzs updatePrimaryIndex(){
		return updatePrimaryIndex(this);
	}
	
	public static List<Yhzs> updatePrimaryIndex(List<Yhzs> yhzss){
		for(Yhzs yhzs : yhzss){
			updatePrimaryIndex(yhzs);	
		}
		return yhzss;
	}
	
	public static Yhzs updatePrimaryIndex(Yhzs yhzs){
		String field = SK_Plus.b(yhzs.getId()).e();
		yhzs.setId_Unique(field);
		
		field = SK_Plus.b(yhzs.getYh_id()).e();
		yhzs.setYh_id_Index(field);
		
		return yhzs;
	}
	
	
	/**
	 * 需要更新字段集
	 */
	private Map<String, Object> updateColumns;

	/* 可能需要补救的字段 */
	private SK_ByteBuff byteBuff = SK_ByteBuff.allocate();
	
	public SK_ByteBuff getSK_ByteBuff(){
		return this.byteBuff;
	}
	
	/** id */
	private int id;
	
	/** 装饰id */
	private int zsid;
	
	/** x */
	private int x;
	
	/** y */
	private int y;
	
	/** 用户_id */
	private int yh_id;
	
	
	public Yhzs() {
		super();
		this._source = true;
	}
	
	public Yhzs(int id, int zsid, int x, int y, int yh_id) {
		super();
		this.id = id;
		this.zsid = zsid;
		this.x = x;
		this.y = y;
		this.yh_id = yh_id;
		this._source = true;
	}
	
	public Map<String, Object> getUpdateColumns() {
		if(updateColumns == null)
			updateColumns = new HashMap<String, Object>();
		return updateColumns;
	}
	
	public void setUpdateColumns(Map<String, Object> updateColumns) {
		this.updateColumns = updateColumns;
	}
	
	public int getId() {
		return id;
	}
	
	public void setId(int id) {
		changeSetId(id);
		if(!this._source){
			byteBuff.writeInt(1);
			byteBuff.writeInt(id);
		}
	}
	
	private void changeSetId(int id) {
		this.id = id;
		if(!this._source){
			addUpdateColumn("id",id);
		}
	}
	
	public void changeIdWith(int id){
		this.id += id;
		changeSetId(this.id);
		//不是源对象才记录变更操作
		if(!this._source){
			byteBuff.writeInt(2);
			byteBuff.writeInt(id);
		}
	}
	public void changeIdWithMin(int id,int min){
		this.id += id;
		this.id = this.id < min ? min : this.id;
		changeSetId(this.id);
		//不是源对象才记录变更操作
		if(!this._source){
			byteBuff.writeInt(3);
			byteBuff.writeInt(id);
			byteBuff.writeInt(min);
		}	
	}
	public void changeIdWithMax(int id,int max){
		this.id += id;
		this.id = this.id > max ? max : this.id;
		changeSetId(this.id);
		//不是源对象才记录变更操作
		if(!this._source){
			byteBuff.writeInt(4);
			byteBuff.writeInt(id);
			byteBuff.writeInt(max);
		}
	}
	public void changeIdWithMaxMin(int id,int max,int min){
		this.id += id;
		this.id = this.id < min ? min : this.id;
		this.id = this.id > max ? max : this.id;
		changeSetId(this.id);
		//不是源对象才记录变更操作
		if(!this._source){
			byteBuff.writeInt(5);
			byteBuff.writeInt(id);
			byteBuff.writeInt(max);
			byteBuff.writeInt(min);
		}	
	}	
	public int getZsid() {
		return zsid;
	}
	
	public void setZsid(int zsid) {
		changeSetZsid(zsid);
		if(!this._source){
			byteBuff.writeInt(6);
			byteBuff.writeInt(zsid);
		}
	}
	
	private void changeSetZsid(int zsid) {
		this.zsid = zsid;
		if(!this._source){
			addUpdateColumn("装饰id",zsid);
		}
	}
	
	public void changeZsidWith(int zsid){
		this.zsid += zsid;
		changeSetZsid(this.zsid);
		//不是源对象才记录变更操作
		if(!this._source){
			byteBuff.writeInt(7);
			byteBuff.writeInt(zsid);
		}
	}
	public void changeZsidWithMin(int zsid,int min){
		this.zsid += zsid;
		this.zsid = this.zsid < min ? min : this.zsid;
		changeSetZsid(this.zsid);
		//不是源对象才记录变更操作
		if(!this._source){
			byteBuff.writeInt(8);
			byteBuff.writeInt(zsid);
			byteBuff.writeInt(min);
		}	
	}
	public void changeZsidWithMax(int zsid,int max){
		this.zsid += zsid;
		this.zsid = this.zsid > max ? max : this.zsid;
		changeSetZsid(this.zsid);
		//不是源对象才记录变更操作
		if(!this._source){
			byteBuff.writeInt(9);
			byteBuff.writeInt(zsid);
			byteBuff.writeInt(max);
		}
	}
	public void changeZsidWithMaxMin(int zsid,int max,int min){
		this.zsid += zsid;
		this.zsid = this.zsid < min ? min : this.zsid;
		this.zsid = this.zsid > max ? max : this.zsid;
		changeSetZsid(this.zsid);
		//不是源对象才记录变更操作
		if(!this._source){
			byteBuff.writeInt(10);
			byteBuff.writeInt(zsid);
			byteBuff.writeInt(max);
			byteBuff.writeInt(min);
		}	
	}	
	public int getX() {
		return x;
	}
	
	public void setX(int x) {
		changeSetX(x);
		if(!this._source){
			byteBuff.writeInt(11);
			byteBuff.writeInt(x);
		}
	}
	
	private void changeSetX(int x) {
		this.x = x;
		if(!this._source){
			addUpdateColumn("x",x);
		}
	}
	
	public void changeXWith(int x){
		this.x += x;
		changeSetX(this.x);
		//不是源对象才记录变更操作
		if(!this._source){
			byteBuff.writeInt(12);
			byteBuff.writeInt(x);
		}
	}
	public void changeXWithMin(int x,int min){
		this.x += x;
		this.x = this.x < min ? min : this.x;
		changeSetX(this.x);
		//不是源对象才记录变更操作
		if(!this._source){
			byteBuff.writeInt(13);
			byteBuff.writeInt(x);
			byteBuff.writeInt(min);
		}	
	}
	public void changeXWithMax(int x,int max){
		this.x += x;
		this.x = this.x > max ? max : this.x;
		changeSetX(this.x);
		//不是源对象才记录变更操作
		if(!this._source){
			byteBuff.writeInt(14);
			byteBuff.writeInt(x);
			byteBuff.writeInt(max);
		}
	}
	public void changeXWithMaxMin(int x,int max,int min){
		this.x += x;
		this.x = this.x < min ? min : this.x;
		this.x = this.x > max ? max : this.x;
		changeSetX(this.x);
		//不是源对象才记录变更操作
		if(!this._source){
			byteBuff.writeInt(15);
			byteBuff.writeInt(x);
			byteBuff.writeInt(max);
			byteBuff.writeInt(min);
		}	
	}	
	public int getY() {
		return y;
	}
	
	public void setY(int y) {
		changeSetY(y);
		if(!this._source){
			byteBuff.writeInt(16);
			byteBuff.writeInt(y);
		}
	}
	
	private void changeSetY(int y) {
		this.y = y;
		if(!this._source){
			addUpdateColumn("y",y);
		}
	}
	
	public void changeYWith(int y){
		this.y += y;
		changeSetY(this.y);
		//不是源对象才记录变更操作
		if(!this._source){
			byteBuff.writeInt(17);
			byteBuff.writeInt(y);
		}
	}
	public void changeYWithMin(int y,int min){
		this.y += y;
		this.y = this.y < min ? min : this.y;
		changeSetY(this.y);
		//不是源对象才记录变更操作
		if(!this._source){
			byteBuff.writeInt(18);
			byteBuff.writeInt(y);
			byteBuff.writeInt(min);
		}	
	}
	public void changeYWithMax(int y,int max){
		this.y += y;
		this.y = this.y > max ? max : this.y;
		changeSetY(this.y);
		//不是源对象才记录变更操作
		if(!this._source){
			byteBuff.writeInt(19);
			byteBuff.writeInt(y);
			byteBuff.writeInt(max);
		}
	}
	public void changeYWithMaxMin(int y,int max,int min){
		this.y += y;
		this.y = this.y < min ? min : this.y;
		this.y = this.y > max ? max : this.y;
		changeSetY(this.y);
		//不是源对象才记录变更操作
		if(!this._source){
			byteBuff.writeInt(20);
			byteBuff.writeInt(y);
			byteBuff.writeInt(max);
			byteBuff.writeInt(min);
		}	
	}	
	public int getYh_id() {
		return yh_id;
	}
	
	public void setYh_id(int yh_id) {
		changeSetYh_id(yh_id);
		if(!this._source){
			byteBuff.writeInt(21);
			byteBuff.writeInt(yh_id);
		}
	}
	
	private void changeSetYh_id(int yh_id) {
		this.yh_id = yh_id;
		if(!this._source){
			addUpdateColumn("用户_id",yh_id);
		}
	}
	
	public void changeYh_idWith(int yh_id){
		this.yh_id += yh_id;
		changeSetYh_id(this.yh_id);
		//不是源对象才记录变更操作
		if(!this._source){
			byteBuff.writeInt(22);
			byteBuff.writeInt(yh_id);
		}
	}
	public void changeYh_idWithMin(int yh_id,int min){
		this.yh_id += yh_id;
		this.yh_id = this.yh_id < min ? min : this.yh_id;
		changeSetYh_id(this.yh_id);
		//不是源对象才记录变更操作
		if(!this._source){
			byteBuff.writeInt(23);
			byteBuff.writeInt(yh_id);
			byteBuff.writeInt(min);
		}	
	}
	public void changeYh_idWithMax(int yh_id,int max){
		this.yh_id += yh_id;
		this.yh_id = this.yh_id > max ? max : this.yh_id;
		changeSetYh_id(this.yh_id);
		//不是源对象才记录变更操作
		if(!this._source){
			byteBuff.writeInt(24);
			byteBuff.writeInt(yh_id);
			byteBuff.writeInt(max);
		}
	}
	public void changeYh_idWithMaxMin(int yh_id,int max,int min){
		this.yh_id += yh_id;
		this.yh_id = this.yh_id < min ? min : this.yh_id;
		this.yh_id = this.yh_id > max ? max : this.yh_id;
		changeSetYh_id(this.yh_id);
		//不是源对象才记录变更操作
		if(!this._source){
			byteBuff.writeInt(25);
			byteBuff.writeInt(yh_id);
			byteBuff.writeInt(max);
			byteBuff.writeInt(min);
		}	
	}	
	
	//用户_id
	public Yh getYhPkYh_id(){
		return YhImp.getById(yh_id);
	}
	public void addUpdateColumn(String key, Object val) {
		Map<String, Object> map = getUpdateColumns();
		if (map == null)
			return;
		map.put(key, val);
	}
	/**
	public void clearUpdateColumn() {
		Map<String, Object> map = getUpdateColumns();
		if (map == null)
			return;
		map.clear();
	}
	*/
	public Yhzs clone(){
		Yhzs yhzs = new Yhzs();
		yhzs.setId(this.getId());
		yhzs.setZsid(this.getZsid());
		yhzs.setX(this.getX());
		yhzs.setY(this.getY());
		yhzs.setYh_id(this.getYh_id());
		yhzs._v = _v;
		yhzs._source = false;
		return yhzs.updatePrimaryIndex(); 
	}
	
	public Map<String, Object> toMap(){
        Map<String, Object> result = new HashMap<String, Object>();
        result.put("id", this.id);
        result.put("zsid", this.zsid);
        result.put("x", this.x);
        result.put("y", this.y);
        result.put("yh_id", this.yh_id);
        return result;
    }
    
    public String toString(){
        return toMap().toString();
    }
    
    public String toJson(){
    	return JSON.toJSONString(toMap());
    }
    
    /**
     * 数据库源字段Map
     */
    public Map<String, Object> toColumnNameMap(){
        Map<String, Object> result = new HashMap<String, Object>();
        result.put("id", this.id);
        result.put("装饰id", this.zsid);
        result.put("x", this.x);
        result.put("y", this.y);
        result.put("用户_id", this.yh_id);
        return result;
    }
    
    public String toColumnNameString(){
        return toColumnNameMap().toString();
    }
    
    public byte[] toBytes() throws Exception {
    	SK_ByteBuff byteBuff = SK_ByteBuff.allocate();
	    byteBuff.writeInt(this.id);
	    byteBuff.writeInt(this.zsid);
	    byteBuff.writeInt(this.x);
	    byteBuff.writeInt(this.y);
	    byteBuff.writeInt(this.yh_id);
	    return byteBuff.toByte();
    }
    
    public byte[] toSKBytes(){
    	SK_ByteBuff byteBuff = SK_ByteBuff.allocate();
    	byteBuff.writeMap(toMap());
	    return byteBuff.toByte();
    }
    
     public static Yhzs createForBytes(byte[] _byte) throws Exception {
     	SK_ByteBuff byteBuff = SK_ByteBuff.wrap(_byte);
     	Yhzs yhzs = new Yhzs();
	    yhzs.id = byteBuff.readInt(null);
	    yhzs.zsid = byteBuff.readInt(null);
	    yhzs.x = byteBuff.readInt(null);
	    yhzs.y = byteBuff.readInt(null);
	    yhzs.yh_id = byteBuff.readInt(null);
	    return yhzs.updatePrimaryIndex();
     }
     
     public static Yhzs createForSKBytes(byte[] _byte){
     		SK_ByteBuff byteBuff = SK_ByteBuff.wrap(_byte);
     		@SuppressWarnings("unchecked")
     		Map<String,Object> map = byteBuff.readMap(null);
	     	Yhzs yhzs = Yhzs.createForMap(map);
		    return yhzs.updatePrimaryIndex();
     }
    
    /**
     * 根据list创建对象
     */
    public static List<Yhzs> createForColumnNameList(List<Map<String, Object>> list){
    	List<Yhzs> yhzss = new ArrayList<Yhzs>();
		for (Map<String, Object> map : list) {
			yhzss.add(createForColumnNameMap(map));
		}
		return yhzss;
    }
    
    /**
     * 根据map创建对象
     */
    public static Yhzs createForColumnNameMap(Map<String, Object> map){
    	Yhzs obj = new Yhzs();
	    obj.id = SK_Map.getInt("id", map);
	    obj.zsid = SK_Map.getInt("装饰id", map);
	    obj.x = SK_Map.getInt("x", map);
	    obj.y = SK_Map.getInt("y", map);
	    obj.yh_id = SK_Map.getInt("用户_id", map);
        return obj.updatePrimaryIndex();
    }
    
    /**
     * 根据list创建对象
     */
    public static List<Yhzs> createForList(List<Map<String, Object>> list){
    	List<Yhzs> yhzss = new ArrayList<Yhzs>();
		for (Map<String, Object> map : list) {
			yhzss.add(createForColumnNameMap(map));
		}
		return yhzss;
    }
    
    /**
     * 根据map创建对象
     */
    public static Yhzs createForMap(Map<String, Object> map){
    	Yhzs obj = new Yhzs();
	    obj.id = SK_Map.getInt("id", map);
	    obj.zsid = SK_Map.getInt("zsid", map);
	    obj.x = SK_Map.getInt("x", map);
	    obj.y = SK_Map.getInt("y", map);
	    obj.yh_id = SK_Map.getInt("yh_id", map);
        return obj.updatePrimaryIndex();
    }
    
    public static Yhzs createForJson(String json){
    	Map<String, Object> map = JSON.parseObject(json);
    	return createForMap(map);
    }
    
    public static List<Yhzs> createForJson(List<String> jsons){
    	List<Yhzs> yhzss = new ArrayList<Yhzs>();
    	for(String json : jsons){
    		yhzss.add(createForJson(json));
    	}
    	return yhzss;
    }
    
    /** 级联删除(延迟入库) */
    public boolean deleteAndSon(){
    	return false;
    }
    
    /** 级联删除(及时入库) */
    public boolean deleteAndSonAndFlush(){
    	return false;
    }
    
    /** 延迟插入数据库 */
    public Yhzs insert(){
    	return YhzsImp.insert(this);
    }
    /** 延迟更新数据库 */
    public Yhzs update(){
    	return YhzsImp.update(this);
    }
    /** 延迟删除数据库 */
    public boolean delete(){
    	return YhzsImp.delete(this);
    }
    /** 即时插入数据库 */
    public Yhzs insertAndFlush(){
    	return YhzsImp.insertAndFlush(this);
    }
    /** 即时更新数据库 */
    public Yhzs updateAndFlush(){
    	return YhzsImp.updateAndFlush(this);
    }
    /** 即时删除数据库 */
    public boolean deleteAndFlush(){
    	return YhzsImp.deleteAndFlush(this);
    }
    
    
    
    //--------------------------------Cache 中的索引 GET & SET
	public String getId_Unique(){
		return id_Unique;
	}
	
	public void setId_Unique(String id_Unique){
		this.id_Unique = id_Unique;
	}
	
	public String getYh_id_Index(){
		return yh_id_Index;
	}
	
	public void setYh_id_Index(String yh_id_Index){
		this.yh_id_Index = yh_id_Index;
	}
	
	
	
	/** 取得改变的索引 */
	public Map<String,String> getUpdateIndexs(){
		Map<String,String> indexNames = new HashMap<String,String>();
		if(!this.getId_Unique().equals(SK_Plus.b(id).e())){
			indexNames.put(Yhzs.Id_IndexName,SK_Plus.b(id).e());
			indexNames.put("isUpdtaeUnique","true");
		}
			
		if(!this.getYh_id_Index().equals(SK_Plus.b(yh_id).e()))
			indexNames.put(Yhzs.Yh_id_IndexName,SK_Plus.b(yh_id).e());
			
		return indexNames;
	}
}